package aos.voting.entity;

import java.io.Serializable;
import java.util.HashMap;

public class ReportData implements Serializable {

	private static final long serialVersionUID = 1L;
	private int nodeId;
	private HashMap<MessageType, Integer> msgRcvNum;
	private HashMap<MessageType, Integer> msgSendNum;

	public ReportData(int nodeId) {
		this.nodeId = nodeId;
		msgRcvNum = new HashMap<MessageType, Integer>();
		msgSendNum = new HashMap<MessageType, Integer>();
		for (MessageType type : MessageType.values()) {
			msgRcvNum.put(type, 0);
			msgSendNum.put(type, 0);
		}
	}

	public synchronized void processMsg(boolean isReceive, Message msg) {
		if (isReceive) {
			msgRcvNum
					.put(msg.getMsgType(), msgRcvNum.get(msg.getMsgType()) + 1);
		} else {
			msgSendNum.put(msg.getMsgType(),
					msgSendNum.get(msg.getMsgType()) + 1);
		}
	}

	public String toString() {
		String s = "nodeId: " + nodeId + " Message Recv: ";
		for (MessageType type : MessageType.values()) {
			s += type + "(" + msgRcvNum.get(type) + ") ";
		}
		s += " Message Sent: ";
		for (MessageType type : MessageType.values()) {
			s += type + "(" + msgSendNum.get(type) + ") ";
		}
		return s;
	}

	public void dump() {
		System.out.println(toString());
	}

	public int getNodeId() {
		return nodeId;
	}

	public void setNodeId(int nodeId) {
		this.nodeId = nodeId;
	}

}
